/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CSISL4pk;

//import javax.swing.JInternalFrame;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
/**
 *
 * @author Josh
 */
public class LoginSubmitSQL {
    public Boolean loginOK = false;
    private ResultSet rs;
    private String qName;
    private String pName;
    public int uID;
    public String uName;
    public String pWd;
    private String perms = "false";
    public String lastLoginDatetime;
    
    public LoginSubmitSQL(){
         //take input from userName.textfield and passWord.textfield
        //connect to db and ask for U & P
        //check the passWord and P, iff == then return true
       //iN = true;
        //if false iN = false;
    }
    
    public Boolean LoginPerms(){
        if(perms.equals("true"))
            return true;
        else
            return false;
    }
    
    public void submitIT (String u, String p){
        
        uName = u;
        pWd = p;
    
        qName = null;
        pName = null;
        perms = "false";
        
        
        try
        {


            // create our mysql database connection
            String myDriver = "com.mysql.jdbc.Driver";
            //String myUrl = "jdbc:mysql://SQL09.FREEMYSQL.NET:3306/csisl";
            String myUrl = "jdbc:mysql://www.db4free.net:3306/csisl";//www.freesql.org:3306/csisl?allowMultiQueries=true";
            Class.forName(myDriver);
            //Connection conn = DriverManager.getConnection(myUrl, "pablofig","cs2450");
            Connection conn = DriverManager.getConnection(myUrl, "csisaltlake","cs2450");
            // our SQL SELECT query. 
            // if you only need a few columns, specify them by name instead of using "*"
            String query = "SELECT username, password, permissions, id FROM users WHERE username = '"+uName+"'";
            
            System.out.print(query);
            // create the java statement
            Statement st = conn.createStatement();
            Statement st2 = conn.createStatement();
            // execute the query, and get a java resultset
            rs = st.executeQuery(query);
            int i = 0;
            // iterate through the java resultset
            while (rs.next())
                {
                    qName = rs.getString("username");
                    pName = rs.getString("password");
                    perms = rs.getString("permissions");
                    uID = rs.getInt("id");
                    System.out.format(":::%s, %s, %s,%d\n", qName, pName,perms, uID);
                }
            String q2 = "select max(endTime) as lastLogin from session where userId ="+uID;//Last login
            if (pName.equals(pWd)){

                System.out.print("\n"+pName+" "+ pWd);
                st.close();
                loginOK = true;
            }
            else{

                //jLabel3.setText("Error: Username or Password invalid");   
                 System.out.print(pName+" "+ pWd+" does not equal DB");
                 st.close();
                 loginOK = false; 
                    }
            ResultSet rs2 = st2.executeQuery(q2);
            while (rs2.next())
            {
                lastLoginDatetime = rs2.getString("lastLogin");
                System.out.println();
                System.out.format("LastLogin: %s\n", lastLoginDatetime );
            }
            st2.close();
        }
        catch (Exception e)
        {
            //print out no such name in db
            System.out.print(e);
             System.out.print("SQL ERROR!!!");
            //welcomeName.setText("No Username in Database!!");
            //JOptionPane.showMessageDialog(this, "No Username in Database!!", "Login Error", JOptionPane.ERROR_MESSAGE);
        }
            
    }
}
